<?php
namespace App\Http\Controllers\User;

use App\Models\Group;
use App\Models\User;
use App\Models\Attendence;
use App\Models\Performance;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Admin\BaseController as BaseController;
use Illuminate\Support\Facades\Auth;


class SettingController extends BaseController
{


    public function __construct() {
        parent::__construct();
    }


    public function getIndex(Request $request)
    {
        $user = $this->user;
        return view('user.kaohe.user-center')
            ->with('user',$user);
    }



    public function postPassword(Request $request)
    {
        $old = $request->input('old','');
        $new = $request->input('newp','');
        if(empty($old) || empty($new)){
            return Response::json(array('status'=>'fail','msg'=>'原密码或新密码不能为空！'));
        }
        $userid = $request->input('uid',0);
        if($userid <=0){
            return Response::json(array('status'=>'fail','msg'=>'用户无效！'));
        }
        $user = User::find($userid);
        if(empty($user)){
            return Response::json(array('status'=>'fail','msg'=>'用户无效！'));
        }
        if(password_verify($old,$user->password) !=1){
            return Response::json(array('status'=>'fail','msg'=>'原密码不正确！'));
        }
        $user->password = bcrypt(trim($new));
        $user->save();
        return Response::json(array('status'=>'success','msg'=>'密码修改成功！'));
    }


    public function postImg(Request $request)
    {
        $uid = $request->input('uid','0');
        $img = $request->input('dataURL','');
        if(empty($uid) || empty($img)){
            return Response::json(array('status'=>'fail','msg'=>'图像不能为空！'));
        }
        if($uid <=0){
            return Response::json(array('status'=>'fail','msg'=>'用户无效！'));
        }
        $user = User::find($uid);
        if(empty($user)){
            return Response::json(array('status'=>'fail','msg'=>'用户无效！'));
        }
        if (strstr($img,",")){
            $img = explode(',',$img);
            $img = $img[1];
        }
        $imageName = "user".date("His",time())."_".rand(1111,9999).'.png';
        $logo = "images/kaohe/". $imageName;
        $r = file_put_contents(public_path($logo), base64_decode($img));
        if (!$r) {
            return Response::json(array('status'=>'fail','msg'=>'图像上传失败！'));
        }else{
            $user->logo = $logo;
            $user->save();
            return Response::json(array('status'=>'success','msg'=>'头像上传成功！'));
        }
    }



}


